package com.banyuan.课堂练习.IO流;

import java.io.*;

/*
 * 将aaa.txt的偶数行和bbb.txt的奇数行交错合并到ccc.txt中；
 * 如果有一个已经到了末尾就之久copy剩下的；*/
public class Ex2 {
    public static void main(String[] args) {
        try (BufferedReader br1 = new BufferedReader(new FileReader("aaa.txt"));
             BufferedReader br2 = new BufferedReader(new FileReader("bbb.txt"));
             BufferedWriter bw = new BufferedWriter(new FileWriter("ccc.txt"))
        ) {
            String s1;
            String s2;
            int i = 1;

            while ((s1 = br1.readLine()) != null && (s2 = br2.readLine()) != null) {
                bw.write(i % 2 == 1 ? s1 : s2);
                bw.newLine();
                bw.flush();
                i++;
            }
            if ((s1 = br1.readLine()) != null) {
                bw.write(s1);
                bw.newLine();
                bw.flush();
            } else if ((s2 = br2.readLine()) != null) {
                bw.write(s2);
                bw.newLine();
                bw.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
